package com.nuoniu.sibanyun.service.erp.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.lang.StringUtils;
import com.nuoniu.sibanyun.entity.erp.ErpSaleType;
import com.nuoniu.sibanyun.entity.erp.in.InRdStyle;
import com.nuoniu.sibanyun.mapper.erp.ErpSaleTypeMapper;
import com.nuoniu.sibanyun.mapper.so.VSaleOrderMainMapper;
import com.nuoniu.sibanyun.service.erp.IErpAccountService;
import com.nuoniu.sibanyun.service.erp.IErpSaleTypeService;
import com.nuoniu.sibanyun.service.erp.in.IInRdStyleService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 小啊刚
 * @since 2020-12-08
 */
@Service
public class ErpSaleTypeServiceImpl extends ServiceImpl<ErpSaleTypeMapper, ErpSaleType> implements IErpSaleTypeService {

    @Autowired
    private IErpAccountService accountService;
    @Autowired
    private VSaleOrderMainMapper orderMainMapper;
    @Autowired
    private IInRdStyleService rdStyleService;

    @Override
    public Page<ErpSaleType> pageList(Page pages, ErpSaleType erpSaleType) {
        UserInfo userInfo = UserThreadLocal.get();
        QueryWrapper<ErpSaleType> queryWrapper = new QueryWrapper();
        queryWrapper.eq("account_id", accountService.getCurrentAccountId());
        queryWrapper.eq("company_id", userInfo.getCompanyId());
        if(StringUtils.isNotBlank(erpSaleType.getSearchKey())) {
            queryWrapper.and(wrapper -> wrapper.likeRight("code", erpSaleType.getSearchKey()).or().like("name", erpSaleType.getSearchKey()));
        }
        Page page = page(pages, queryWrapper);
        return page;
    }

    @Override
    public void saveSaleType(ErpSaleType erpSaleType) {
        //ErpSaleType saleTypeByName = baseMapper.getByName(erpSaleType);
        if(erpSaleType.getId() != null) {
            //List<VSaleOrderMain> orderMains = orderMainMapper.getBySaleType(erpSaleType.getId());
        }else {
            erpSaleType.setAccountId(accountService.getCurrentAccountId());
            erpSaleType.setCompanyId(UserThreadLocal.get().getCompanyId());
        }
        if(isRepeatCode(erpSaleType.getCode(),erpSaleType.getId())) {
            throw new NuoNiuException("编号重复");
        }
        if(isRepeatName(erpSaleType.getName(),erpSaleType.getId())) {
            throw new NuoNiuException("名称重复");
        }

        if(null != erpSaleType.getIsDefaultValue() && erpSaleType.getIsDefaultValue()) {
            //重置 默认值
            baseMapper.updateDefaultValue(accountService.getCurrentAccountId());
        }
//        InRdStyle rdStyle = rdStyleService.getById(erpSaleType.getStyleId());
//        erpSaleType.setStyleName(rdStyle.getName());
        this.saveOrUpdate(erpSaleType);
    }

    @Override
    public void deleteById(List<Long> ids) {
        this.removeByIds(ids);
    }

    @Override
    public Integer getByStyle(Long id) {
        Integer accountId = accountService.getCurrentAccountId();
        return baseMapper.getByStyle(id, accountId);
    }

    @Override
    public boolean isRepeatCode(String code,Long oldId) {
        UserInfo userInfo = UserThreadLocal.get();
        QueryWrapper<ErpSaleType> queryWrapper =  new QueryWrapper();
        queryWrapper.eq("company_id",userInfo.getCompanyId());
        queryWrapper.eq("code",code);
        if(null != oldId){
            queryWrapper.ne("id",oldId);
        }
        int count = count(queryWrapper);
        return count > 0;
    }

    @Override
    public boolean isRepeatName(String name,Long oldId) {
        UserInfo userInfo = UserThreadLocal.get();
        QueryWrapper<ErpSaleType> queryWrapper =  new QueryWrapper();
        queryWrapper.eq("company_id",userInfo.getCompanyId());
        queryWrapper.eq("name",name);
        if(null != oldId){
            queryWrapper.ne("id",oldId);
        }
        int count = count(queryWrapper);
        return count > 0;
    }
}
